package com.edee.bigday.commodity.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@EnableConfigurationProperties(CacheProperties.class)
@Configuration
@EnableCaching
//@EnableCaching从com.edee.bigday.commodity.BigdayCommodityApplication移来。
public class MyCacheConfig {
    @Autowired
    CacheProperties cacheProperties;
    /**
     * 测试发现，配置文件application.properties中的内容没有用上，TTL=-1；
     * 1）原来和配置文件绑定的配置类是下面这样的：
     *      @ConfigurationProperties(prefix = "spring.cache")
     *      public class CacheProperties
     *      因为现在并未放在容器中，所以，并不起作用。
     * 2）现在要让它生效，需要添加注解@EnableConfigurationProperties(CacheProperties.class),
     *      这样就可以让配置文件和CacheProperties绑定生效。
     *      生效之后有两种使用方法：
     *      方法一：自动注入：
     *      @Autowired
     *     CacheProperties cacheProperties;
     *     方法二：将CacheProperties cacheProperties作为参数放入方法签名中；
     *          本例用方法二。
     *          RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties)
     *          因为该方法本来就是要往容器中放入内容，方法传的所有参数，都会从容器中确定（？？？）。
     * @return
     */
    @Bean
    RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
//        config = config.entryTtl(Duration.ofSeconds(50));
        config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
        //Ctrl+单击上面的StringRedisSerializer，进入org.springframework.data.redis.serializer.StringRedisSerializer,其中有：
        //public class StringRedisSerializer implements RedisSerializer<String> {...
        //在RedisSerializer处Ctrl+h，可以看到继承关系，和JSON有关的如下：
        //GenericJackson2JsonRedisSerializer (org.springframework.data.redis.serializer)
        //FastJsonRedisSerializer (com.alibaba.fastjson.support.spring)
        //FastJsonRedisSerializer (com.alibaba.fastjson.support.spring)
        //GenericToStringSerializer (org.springframework.data.redis.serializer)
        //StringRedisSerializer (org.springframework.data.redis.serializer)
        //GenericFastJsonRedisSerializer (com.alibaba.fastjson.support.spring)
        //GenericFastJsonRedisSerializer (com.alibaba.fastjson.support.spring)
        //JdkSerializationRedisSerializer (org.springframework.data.redis.serializer)
        //Jackson2JsonRedisSerializer (org.springframework.data.redis.serializer)
        //其中，带有Generic可以转为任意对象，因此选用此类。

        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericFastJsonRedisSerializer()));
        //config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
        //以上GenericFastJsonRedisSerializer和GenericJackson2JsonRedisSerializer均可。
        //使配置文件application.properties中的所有配置生效：
        //将org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration中的判断语句复制过来：
        //从配置文件中拿到和Redis有关的所有配置：
        CacheProperties.Redis redisProperties = cacheProperties.getRedis();
        if (redisProperties.getTimeToLive() != null) {
            config = config.entryTtl(redisProperties.getTimeToLive());
        }
        if (redisProperties.getKeyPrefix() != null) {
            config = config.prefixKeysWith(redisProperties.getKeyPrefix());
        }
        if (!redisProperties.isCacheNullValues()) {
            config = config.disableCachingNullValues();
        }
        if (!redisProperties.isUseKeyPrefix()) {
            config = config.disableKeyPrefix();
        }
        return config;
    }
}
